package com.pk.platform.business.privilege.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.pk.platform.business.core.web.GenericAction;
import com.pk.platform.business.privilege.service.IDeptService;
import com.pk.platform.business.privilege.service.IRoleService;
import com.pk.platform.business.privilege.service.IUserService;
import com.pk.platform.business.privilege.vo.UserVO;
import com.pk.platform.domain.privilege.Department;
import com.pk.platform.domain.privilege.Role;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.MD5;
import com.pk.platform.util.constant.Constant;

public class UserAction extends GenericAction {

	private User user = new User();
	
	private UserVO uvo = new UserVO();
	
	private IUserService userService;
	
	private String[] userIds;
	
	private List<Department> deptList = new ArrayList<Department>();
	
	private IDeptService deptService;
	
	private boolean passwordFlag = false;
	
	private String userRoleIds;
	
	private List<Role> roleList = new ArrayList<Role>();
	
	private IRoleService roleService;
	
	private String[] roleIds;
	
	private boolean addUserFlag = false;
	
	private boolean operateUser = false;
	
	public String toLogin()
	{
		return SUCCESS;
	}
	
	public String login()
	{
		User loginUser = userService.login(user.getId(), user.getPassword());
		if(loginUser != null)
		{
			this.getSession().setAttribute(Constant.USER, loginUser);
			return SUCCESS;
		}
		message = "提示：用户名或密码错误。";
		return ERROR;
	}
	
	public String logout()
	{
		this.getSession().setAttribute(Constant.USER, null);
		return SUCCESS;
	}
	
	public String toAddUser()
	{
		roleList = roleService.queryRoleList();
		deptList = deptService.queryDeptList();
		return SUCCESS;
	}
	
	public String addUser()
	{
		user.setRegisterTime(new Date());
		addUserFlag = userService.addUser(user, roleIds);
		return SUCCESS;
	}
	
	public String delUsers()
	{
		if(userIds != null)
		{
			userService.delUsers(userIds);
		}
		this.sendRedirect("inner/user/queryUser.action");
		return SUCCESS;
	}
	

	public String toUpdateUser()
	{
		deptList = deptService.queryDeptList();
		user = userService.queryUserById(user.getId());
		return SUCCESS;
	}
	
	public String updateUser()
	{
		userService.updateUser(user);
		return SUCCESS;
	}

	public String queryUser()
	{
		User loginUser = this.getLoginUser();
		if(Constant.POWER_USER.equals(loginUser.getId()))
		{
			operateUser = true;
		}
		if(uvo.getUser().getUseType() == null)
		{
			uvo.getUser().setUseType("");
		}
		deptList = deptService.queryDeptList();
		list = userService.queryUser(uvo, pager);
		return SUCCESS;
	}
	
	public String useUsersOrNot()
	{
		if(userIds != null)
		{
			userService.useUsersOrNot(userIds, user.getUseType());
		}
		this.sendRedirect("inner/user/queryUser.action");
		return SUCCESS;
	}
	
	public String toUpdatePassword()
	{
		return SUCCESS;
	}
	
	public String updatePassword()
	{
		String encodePassword = MD5.getMD5(uvo.getBeforePassword());
		User loginUser = this.getLoginUser();
		if(loginUser.getPassword().equals(encodePassword))
		{
			passwordFlag = true;
			String newEncodePassword = MD5.getMD5(user.getPassword());
			userService.updatePasswordByUserId(loginUser.getId(), user.getPassword());
			loginUser.setPassword(newEncodePassword);
			this.getSession().setAttribute(Constant.USER, loginUser);
		}
		return SUCCESS;
	}
	
	public String toResetPassword()
	{
		user = userService.queryUserById(user.getId());
		return SUCCESS;
	}
	
	public String resetPassword()
	{
		userService.updatePasswordByUserId(user.getId(), user.getPassword());
		return SUCCESS;
	}
	
	public String toAssignRole()
	{
		user = userService.queryUserById(user.getId());
		userRoleIds = userService.queryUserRoleIdString(user.getId());
		roleList = roleService.queryRoleList();
		return SUCCESS;
	}
	
	public String assignRole()
	{
		userService.assignRole(user.getId(), roleIds);
		return SUCCESS;
	}
	
	
	
	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserVO getUvo() {
		return uvo;
	}

	public void setUvo(UserVO uvo) {
		this.uvo = uvo;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String[] getUserIds() {
		return userIds;
	}

	public void setUserIds(String[] userIds) {
		this.userIds = userIds;
	}

	public List<Department> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Department> deptList) {
		this.deptList = deptList;
	}

	public IDeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(IDeptService deptService) {
		this.deptService = deptService;
	}

	public boolean isPasswordFlag() {
		return passwordFlag;
	}

	public void setPasswordFlag(boolean passwordFlag) {
		this.passwordFlag = passwordFlag;
	}

	public String getUserRoleIds() {
		return userRoleIds;
	}

	public void setUserRoleIds(String userRoleIds) {
		this.userRoleIds = userRoleIds;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public IRoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public String[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String[] roleIds) {
		this.roleIds = roleIds;
	}

	public boolean isAddUserFlag() {
		return addUserFlag;
	}

	public void setAddUserFlag(boolean addUserFlag) {
		this.addUserFlag = addUserFlag;
	}

	public boolean isOperateUser() {
		return operateUser;
	}

	public void setOperateUser(boolean operateUser) {
		this.operateUser = operateUser;
	}
	
}
